package com.lhgray.sglhome.domain.usecase

import com.freightoa.foundation.net.ApiResult
import com.freightoa.foundation.net.bean.SGLResponse
import com.freightoa.foundation.presentation.domain.BaseUseCase
import com.lhgray.sglhome.domain.model.SourceData
import com.lhgray.sglhome.data.repository.PicUploadRepository
import javax.inject.Inject

/**
 * 作者：lhgray on 2025/3/27.
 * 邮箱：lhgray@163.com
 * 文件：com.lhgray.sglhome.domain
 */
class OrderListUseCase @Inject constructor(private val picUploadRepository: PicUploadRepository) :
    BaseUseCase() {
    suspend operator fun invoke(fileNo: String): Result<SourceData?> {

        return when (val orderList = picUploadRepository.getOrderList(fileNo)) {
            is ApiResult.Error -> Result.failure(RuntimeException(orderList.message))
            is ApiResult.Exception -> Result.failure(orderList.throwable)
            is ApiResult.Success -> {
                processSuccessResult(orderList)

            }
        }
    }

    private fun processSuccessResult(result: ApiResult.Success<SGLResponse<SourceData>>): Result<SourceData> {
        // 获取数据
        val data = result.data.data ?: return Result.failure(NullPointerException("data is null"))
        // 检查数据是否为空
        // 获取列表项标签
        val listItemLabel = data.list_item_label
        // 检查列表项标签是否为空
        if (listItemLabel.isEmpty()) {
            return Result.failure(NullPointerException("data is null"))
        } else {
            return Result.success(data)
        }
    }

}
